import BaseShape2D from "./BaseShape2D";
import Vector2 from "../maths/Vector2";
import ITransformable from "../interfaces/ITransformable";
import IRenderState from "../interfaces/IRenderState";

export default class BezierPath extends BaseShape2D {
    // 指向参数曲线的控制点集数据源
    public points: Vector2[];
    // 是二次还是三次贝塞尔曲线路径
    public isCubic: boolean;

    public constructor(points: Vector2[], isCubic: boolean = false) {
        super();
        this.points = points;
        this.isCubic = isCubic;
        this.data = points;
    }

    public get type(): string {
        return "BezierPath";
    }

    // 不支持贝塞尔曲线的碰撞检测，直接返回false，这样就会跳过碰撞检测，鼠标点选就永远不会点中
    public hitTest(localPt: Vector2, transform: ITransformable): boolean { return false; }

    public draw(transformable: ITransformable, state: IRenderState, context: CanvasRenderingContext2D): void {
        context.beginPath();
        context.moveTo(this.points[0].x, this.points[0].y);
        if (this.isCubic) {
            // 绘制三次贝塞尔路径
            for (let i = 1; i < this.points.length; i += 3) {
                context.bezierCurveTo(this.points[i].x,
                    this.points[i].y,
                    this.points[i + 1].x,
                    this.points[i + 1].y,
                    this.points[i + 2].x,
                    this.points[i + 2].y);
            }
        } else {
            // 绘制二次贝塞尔路径
            // [ a0 , c1 , a2 , c3 , a4 , c5 , a0 ] 
            for (let i: number = 1; i < this.points.length; i += 2) {
                context.quadraticCurveTo(this.points[i].x,
                    this.points[i].y,
                    this.points[i + 1].x,
                    this.points[i + 1].y);
            }
        }
        // 一定要调用基类方法，进行呈现操作
        super.draw(transformable, state, context);
    }
}